﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DI;
using Front.Controllers.Api;
using Core.Dtos;
using Core.Messages;
using StructureMap;
using Core.Queries;
using StructureMap.AutoMocking;
using System.Transactions;
using Tests.Helpers;
using StructureMap.Pipeline;
using Front.Areas.Admin.Controllers.Api;
using CMS.Repositories;

namespace Insurance.Test
{
    [TestClass]
    public class DictionaryApiControllerTest
    {
        private TransactionScope transScope;
        protected ControllerHelper<DictionaryApiController> controllerHelper;

        [ClassInitialize()]
        void Initialize()
        {            
        }

        [TestInitialize()]
        public void InitializeTest()
        {
            Bootstrapper.Init(new SingletonLifecycle());
            CurrentContextHelper.Logon();
            transScope = new TransactionScope(TransactionScopeOption.Required);
            controllerHelper = new ControllerHelper<DictionaryApiController>();
            controllerHelper.Initialize();
        }

        [TestCleanup()]
        public void CleanupTest()
        {
            controllerHelper.Cleanup();
            Bootstrapper.Resolve<AdminDbContext>().Dispose();
            transScope.Dispose();            
        }

        [TestMethod]
        public void DictionaryApiController_CanCreate()
        {
            // Arrange            
            var controller = controllerHelper.GetController();

            // Act            
            var response = controller.Post(GetSampleDto());

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Create failed");            
        }

        [TestMethod]
        public void DictionaryApiController_CanRead()
        {
            // Arrange            
            var controller = controllerHelper.GetController();
            var id = controller.Post(GetSampleDto()).Value;

            // Act            
            var dto = controller.Get(id);

            // Assert
            Assert.IsNotNull(dto, "Get failed");        
        }

        [TestMethod]
        public void DictionaryApiController_CanEdit()
        {
            // Arrange            
            var controller = controllerHelper.GetController();
            var dto = GetSampleDto();
            dto.Id = controller.Post(dto).Value;

            // Act                        
            dto.Name = "Dictionary2";
            Response response = controller.Put(dto.Id, dto);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Edit failed");
        }

        [TestMethod]
        public void DictionaryApiController_CanDelete()
        {
            // Arrange      
            var controller = controllerHelper.GetController();
            var id = controller.Post(GetSampleDto()).Value;

            // Act
            Response response = controller.Delete(id);

            //Assert            
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Delete failed");
        }

        [TestMethod]
        public void DictionaryApiController_CanFindBy()
        {
            // Arrange            
            var controller = controllerHelper.GetController();
            controller.Post(GetSampleDto());
            var query = new DictionaryQuery() { Name = "Dictionary1" };            

            // Act            
            var response = controller.FindBy(query);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(response.List.Count, 1, "FindBy Failed");           
        }

        public DictionaryDto GetSampleDto()
        {
            return new DictionaryDto() { Name = "Dictionary1" };
        }
    }
}
